﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using SharpMap;

using System.Xml;

using SharpMap.Styles;
using System.Drawing;
using System.IO;

using SharpMap.Layers;

namespace KoalaGIS
{
    public class SharpMapDocument
    {
        public string Name { get; set; }

        public DateTime CreateTime { get; set; }

        public string Author { get; set; }

        public Map Map { get; set; }


        public void XmlSerialize( string filepath  )
        {

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            Stream sm = asm.GetManifestResourceStream("KoalaGIS.SerializerTemplate.xml");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(sm);


            XmlNode nodeLayers = doc.SelectSingleNode("SharpMapDocument//Map//Layers");


            XmlNode centerNode = doc.SelectSingleNode("SharpMapDocument//Map//Center");
            centerNode.SelectSingleNode("X").InnerText = this.Map.Center.X.ToString();
            centerNode.SelectSingleNode("Y").InnerText = this.Map.Center.Y.ToString();

            doc.SelectSingleNode("SharpMapDocument//Map//Zoom").InnerText = this.Map.Zoom.ToString();

            XmlNode node = doc.SelectSingleNode("SharpMapDocument//Map//Layers//Layer");

            nodeLayers.RemoveChild(node);

            foreach (ILayer lyr in this.Map.Layers)
            {

                VectorLayer layer = lyr as VectorLayer;
                if (layer == null)
                    continue;

                SharpMap.Data.Providers.ShapeFile shpfile = layer.DataSource as SharpMap.Data.Providers.ShapeFile;

                node.SelectSingleNode("LayerName").InnerText = layer.LayerName;
                node.SelectSingleNode("DataSource/Type").InnerText = "shp";
                node.SelectSingleNode("DataSource/Path").InnerText = shpfile.Filename;

                VectorStyle style = layer.Style;
                SolidBrush brush = (SolidBrush)style.Fill;

                node.SelectSingleNode("Style/Line/Color").InnerText = style.Line.Color.ToArgb().ToString();
                node.SelectSingleNode("Style/Line/Width").InnerText = style.Line.Width.ToString();

                node.SelectSingleNode("Style/Fill/FillColor").InnerText = brush.Color.ToArgb().ToString();
                node.SelectSingleNode("Style/Fill/OutLine/Width").InnerText = style.Outline.Width.ToString();
                node.SelectSingleNode("Style/Fill/OutLine/Color").InnerText = style.Outline.Color.ToArgb().ToString();

                string xmlContent = node.OuterXml;

                XmlElement eleLayer = doc.CreateElement("Layer");
                eleLayer.InnerXml = node.InnerXml;

                nodeLayers.AppendChild(eleLayer);
            }

            doc.Save(filepath);


            

        }

        public void ReadDocInfo(string filepath, SharpMap.Map map )
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filepath);


            double x = double.Parse( doc.SelectSingleNode("SharpMapDocument//Map//Center//X").InnerText );
            double y = double.Parse(doc.SelectSingleNode("SharpMapDocument//Map//Center//Y").InnerText);
            double zoom = double.Parse(doc.SelectSingleNode("SharpMapDocument//Map//Zoom").InnerText);

            XmlNodeList nodes = doc.SelectNodes("SharpMapDocument//Map//Layers//Layer");


            string datasourcetype = "";
            string datasourcepath = "";
            string layername = "";
            ILayer layer = null;
            foreach (XmlNode node in nodes)
            {
                datasourcetype = node.SelectSingleNode("DataSource/Type").InnerText ;
                layername = node.SelectSingleNode("LayerName").InnerText;
                datasourcepath = node.SelectSingleNode("DataSource/Path").InnerText;
                if (datasourcetype == "shp")
                {
                    layer = new VectorLayer(layername);
                    SharpMap.Data.Providers.ShapeFile shp = new SharpMap.Data.Providers.ShapeFile(datasourcepath);
                    (layer as VectorLayer).DataSource = shp;

                }


                VectorStyle style = new VectorStyle();

                Color line_color = Color.FromArgb( int.Parse(node.SelectSingleNode("Style/Line/Color").InnerText ) );
                float line_width = float.Parse( node.SelectSingleNode("Style/Line/Width").InnerText );
                style.Line = new Pen(line_color, line_width);
                
                Color fill_color = Color.FromArgb( int.Parse( node.SelectSingleNode("Style/Fill/FillColor").InnerText) );
                float out_width = float.Parse( node.SelectSingleNode("Style/Fill/OutLine/Width").InnerText );
                Color out_color = Color.FromArgb( int.Parse( node.SelectSingleNode("Style/Fill/OutLine/Color").InnerText) );

                style.Fill = new SolidBrush(fill_color);
                style.Outline = new Pen(out_color, out_width);

                if (layer is VectorLayer)
                {
                    (layer as VectorLayer).Style = style;
                }

                map.Layers.Add(layer);

                map.Center = new SharpMap.Geometries.Point(x, y);
                map.Zoom = zoom;

            }

            
        }


    }
}
